﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Data;
using MyWeb.Business;
using MyWeb.Common;
using System.Data;

namespace MyWeb.Admins
{
    public partial class Bill : System.Web.UI.Page
    {
        static string Id = "";


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //lbtDeleteT.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa?');");
                //lbtDeleteB.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa?');");

                BindGrid();
            }
        }

        private void BindGrid()
        {
            grdBill.DataSource = BillService.Bill_GetByAll();
            grdBill.DataBind();
            if (grdBill.PageCount <= 1)
            {
                grdBill.PagerStyle.Visible = false;
            }
        }

        //private void LoadCustormerDropDownList()
        //{
        //    ddlCustormer.Items.Clear();
        //    ddlCustormer.Items.Add(new ListItem("", ""));
        //    List<Data.Custormer> list = CustormerService.Custormer_GetByAll();
        //    for (int i = 0; i < list.Count; i++)
        //    {
        //        ddlCustormer.Items.Add(new ListItem(list[].Name, list[i].Id));
        //    }
        //    ddlCustormer.DataBind();
        //}
        //protected void DeleteButton_Click(object sender, EventArgs e)
        //{
        //    DataGridItem item = default(DataGridItem);
        //    for (int i = 0; i < grdBill.Items.Count; i++)
        //    {
        //        item = grdBill.Items[i];
        //        if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
        //        {
        //            if (((CheckBox)item.FindControl("ChkSelect")).Checked)
        //            {
        //                string strId = item.Cells[1].Text;
        //                BillService.Bill_Delete(strId);
        //            }
        //        }
        //    }
        //    grdBill.CurrentPageIndex = 0;
        //    BindGrid();
        //}
        protected void grdBill_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdBill.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex.ToString() + ")");
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex.ToString() + ")");
                    }
                }
            }
        }

        protected void grdBill_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdBill.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void grdBill_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string strCA = e.CommandArgument.ToString();
            this.txtIdCus.Text = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Edit":
                    Id = strCA;

                    List<Data.Bill> listOr = BillService.Bill_GetById(strCA);
                    if (listOr.Count > 0)
                    {
                        List<Data.Custormer> listCus = CustormerService.Custormer_GetById(listOr[0].CustormerId);
                        grdCustormer.DataSource = listCus;
                        grdCustormer.DataBind();
                        listCus.Clear();
                        listCus = null;
                    }
                    List<Data.BillDetail> listOrderDetail = BillDetailService.BillDetail_GetByTop("1000", "Billid = '" + strCA + "'", "");
                    grdShopcard.DataSource = listOrderDetail;
                    grdShopcard.DataBind();
                    Double money = 0;
                    for (int i = 0; i < listOrderDetail.Count; i++)
                    {
                        money += Double.Parse(listOrderDetail[i].Total.ToString());
                    }
                    this.lblsumpay.Text = Format_Frice(money.ToString());
                    listOrderDetail.Clear();
                    listOrderDetail = null;
                    listOr.Clear();
                    listOr = null;

                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;
                case "Delete":
                    SqlDataProvider sql1 = new SqlDataProvider();
                    sql1.ExecuteNonQuery("Delete from BillDetail  Where BillId='" + strCA + "'");
                    BillService.Bill_Delete(strCA);
                    BindGrid();
                    break;

                case "Active":
                    string strA = "";
                    string str = e.Item.Cells[2].Text;
                    strA = str == "1" ? "0" : "1";
                    SqlDataProvider sql = new SqlDataProvider();
                    sql.ExecuteNonQuery("Update [Bill] set Active=" + strA + "  Where Id='" + strCA + "'");
                    BindGrid();
                    break;
            }
        }

        //protected void AddButton_Click(object sender, EventArgs e)
        //{
        //    pnUpdate.Visible = true;
        //    ControlClass.ResetControlValues(this);
        //    LoadCustormerDropDownList();
        //    txtDatesend.Text = DateTimeClass.ConvertDateTime(DateTime.Now, "dd/MM/yyyy hh:mm:ss tt");
        //    txtDateReceive.Text = DateTimeClass.ConvertDateTime(DateTime.Now, "dd/MM/yyyy hh:mm:ss tt");
        //    pnView.Visible = false;
        //    Insert = true;
        //}



        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void Update_Click(object sender, EventArgs e)
        {

            //if (Page.IsValid)
            //{
            //    Data.Bill obj = new Data.Bill();
            //    obj.Id = Id;
            //    //obj.Name = list.Text;
            //    //obj.Totalmoney = NumberClass.ConvertNumber(txtTotalmoney.Text, "en-US");
            //    //obj.Datesend = DateTimeClass.ConvertDateTime(txtDatesend.Text, "MM/dd/yyyy HH:mm:ss");
            //    //obj.DateReceive = DateTimeClass.ConvertDateTime(txtDateReceive.Text, "MM/dd/yyyy HH:mm:ss");
            //    //obj.Request = txtRequest.Text;
            //    obj.Active = chkActive.Checked ? "1" : "0";
            //    //obj.CustormerId = ddlCustormer.SelectedValue;
            //    if (Insert == true)
            //    {
            //        BillService.Bill_Insert(obj);
            //    }
            //    else
            //    {
            //        BillService.Bill_Update(obj);
            //    }
            //    BindGrid();
            //    pnView.Visible = true;
            //    pnUpdate.Visible = false;
            //    Insert = false;            
            //}
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;

        }

        protected string Format_Frice(string money)
        {
            if (money.Length > 3)
            {
                int tien = money.Length;
                while (tien > 3)
                {
                    money = money.Insert(tien - 3, ".");
                    tien = tien - 3;
                }
                return money + "VNĐ";
            }
            else
            {
                return money + "VNĐ";
            }
        }

        protected string Tensp(string ProId)
        {
            List<Data.Product> listpro = ProductService.Product_GetById(ProId);
            return listpro[0].Name.ToString();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            SqlDataProvider sql = new SqlDataProvider();
            string active = "";
            if (chkActive.Checked)
            {
                active = "1";
            }
            else
            {
                active = "0";
            }
            sql.ExecuteNonQuery("Update [Bill] Set Active='" + active + "' Where Id='" + Id + "'");
            pnUpdate.Visible = false;
            pnView.Visible = true;
            BindGrid();
            //if (Page.IsValid)
            //{
            //    Data.Bill obj = new Data.Bill();
            //    obj.Id = Id;
            //    //obj.Name = list.Text;
            //    //obj.Totalmoney = NumberClass.ConvertNumber(txtTotalmoney.Text, "en-US");
            //    //obj.Datesend = DateTimeClass.ConvertDateTime(txtDatesend.Text, "MM/dd/yyyy HH:mm:ss");
            //    //obj.DateReceive = DateTimeClass.ConvertDateTime(txtDateReceive.Text, "MM/dd/yyyy HH:mm:ss");
            //    //obj.Request = txtRequest.Text;
            //    obj.Active = chkActive.Checked ? "1" : "0";
            //    //obj.CustormerId = ddlCustormer.SelectedValue;
            //    if (Insert == true)
            //    {
            //        BillService.Bill_Insert(obj);
            //    }
            //    else
            //    {
            //        BillService.Bill_Update(obj);
            //    }
            //    BindGrid();
            //    pnView.Visible = true;
            //    pnUpdate.Visible = false;
            //    Insert = false;
            //}
        }

        protected void lbtDeleteT_Click(object sender, EventArgs e)
        {
            DataGridItem item = default(DataGridItem);
            for (int i = 0; i < grdBill.Items.Count; i++)
            {
                item = grdBill.Items[i];
                if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                {
                    if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                    {
                        string strId = item.Cells[1].Text;
                        SqlDataProvider sql = new SqlDataProvider();
                        sql.ExecuteNonQuery("Delete from BillDetail  Where BillId='" + strId + "'");
                        BillService.Bill_Delete(strId);
                    }
                }
            }
            grdBill.CurrentPageIndex = 0;
            BindGrid();
        }
    }
}